from myProfile.models.data.myProfile_models import Login, PsychoTherapy
from typing import List


class PsychoTherapyRepository:

    def __init__(self, engine):
        self.engine = engine

    async def add_to_profile(
            self, login_id: int, psycho_therapy: PsychoTherapy) -> bool:
        try:
            login = await self.engine.find_one(Login, Login.id == login_id)
            therapies = login.profiles.psycho_therapy + [psycho_therapy]
            login.profiles.psycho_therapy = therapies
            await self.engine.save(login)
        except Exception as e:
            print(e)
            return False
        return True

    async def remove_from_profile(self, login_id: int, id: int) -> bool:
        try:
            login = await self.engine.find_one(Login, Login.id == login_id)
            psycho_therapy = login.profiles.psycho_therapy
            values = [value for value in psycho_therapy if value.id != id]
            login.profiles.psycho_therapy = values
            await self.engine.save(login)
        except Exception as e:
            print(e)
            return False
        return True

    async def get_from_profile(self, login_id: int) -> List[PsychoTherapy]:
        login = await self.engine.find_one(Login, Login.id == login_id)
        return login.profiles.psycho_therapy
